﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using Dundas.Charting.WebControl;
using Wind.MultimediaStation.WebService.Entity;
using Wind.Utils;

namespace Wind.MultimediaStation.WebService
{
    public class BaseChart
    {
        //过滤行情数据
        public static void FilterQuoteData(StockQuote StockQuote)
        {
#if DEBUG
            //StockQuote.CurrentDate = "13:30";
            //DateTime dt = DateTime.Parse(string.Format("{0} {1}", StockQuote.QuoteStartTime.ToShortDateString(), StockQuote.CurrentDate));


            //List<string> tempDateList= StockQuote.QuoteDateList.FindAll(p => { return DateTime.Parse(p) <= dt; });
            //int tempcount = tempDateList.Count;
            //List<double> tempPriceList = StockQuote.QuotePriceList.Select<double, double>((p, i) =>
            //{
            //    if (i < tempcount) { return p; } else {
            //        return 0;
            //    }

            //}).ToList().FindAll(p=>p!=0);

            //StockQuote.QuoteDateList = tempDateList;
            //StockQuote.QuotePriceList = tempPriceList;

#endif

            if (StockQuote != null && StockQuote.QuoteDateList != null && StockQuote.QuoteDateList.Count>0)
            {
                int count = StockQuote.QuoteDateList.Count - 1;
                DateTime t = DateTime.Now;
                string PauseEnd = "";
                double PauseEndPrice=0.0d;

                for (int i = count; i >= 0; i--)
                {
                    // todo
                    t = DateTime.Parse(string.Format("{0} {1}", StockQuote.QuoteStartTime.ToShortDateString(), StockQuote.QuoteDateList[i]));

                    //小于行情开始时间的数据去掉，大于行情结束时间的数据去掉
                    if (t > StockQuote.QuoteEndTime.AddSeconds(59) || t < StockQuote.QuoteStartTime)
                    {
                        //SeriesStockQuote.Points.RemoveAt(i);
                        StockQuote.QuoteDateList.RemoveAt(i);
                        StockQuote.QuotePriceList.RemoveAt(i);
                    }
                    else {
                        //休市时去掉休市结束点
                        if (StockQuote.QuoteDateList[i].Contains(StockQuote.PauseEndTime.ToShortTimeString()))
                        {
                            PauseEnd = StockQuote.QuoteDateList[i];
                            PauseEndPrice = StockQuote.QuotePriceList[i];
                            StockQuote.QuoteDateList.RemoveAt(i);
                            StockQuote.QuotePriceList.RemoveAt(i);
                        } 
                    }            

                }

                //统一处理一下时间格式是9:00 而不是09:00
                List<string> tempList = StockQuote.QuoteDateList.Distinct().Select(p => { return DateTime.Parse(p).ToShortTimeString(); }).ToList();
                StockQuote.QuoteDateList = tempList;

            }
        }


        //计算Y轴区域
        public static void ComputeYArea(StockQuote StockQuote, ChartArea ChartArea)
        {
            if (StockQuote.QuoteDateList.Count>0)
            {
                double yestodayPrice = StockQuote.YestodayPrice;
                double realYmax = StockQuote.QuotePriceList.Max();
                double realYmin = StockQuote.QuotePriceList.Min();
                double span = realYmax - realYmin;

                double tempY = 1.0 / 2 * span + realYmin;
                if (tempY > yestodayPrice)
                {
                    realYmin = realYmax - 2 * (realYmax - yestodayPrice);
                }
                else
                {
                    realYmax = realYmin + 2 * (yestodayPrice - realYmin);
                }

                //double interval = Math.Round(1.0 / 14 * (realYmax - realYmin), 5);
                double interval = Math.Round(1.0 / 4 * (realYmax - realYmin), 5);

                ChartArea.AxisY.Minimum = realYmin;
                ChartArea.AxisY.Maximum = realYmax + 0.18 * interval;

                ChartArea.AxisY.MajorGrid.Interval = interval;
                ChartArea.AxisY.LabelStyle.Interval = interval;
                ChartArea.AxisY.MajorTickMark.Interval = interval;
            }

            ChartArea.AxisY.LabelStyle.Format = "F2";
            ChartArea.AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("100, 255, 255, 255");//
            ChartArea.AxisY.MajorGrid.LineStyle = ChartDashStyle.Dash;
            ChartArea.AxisY.LabelStyle.FontColor = Color.White;            
            ChartArea.AxisY.LineColor = Color.White;
            ChartArea.AxisY.MajorTickMark.LineColor = ColorTranslator.FromHtml("150, 255, 255, 255");
            ChartArea.AxisY.Margin = false;
            ChartArea.AxisY.MinorGrid.Enabled = false;
            ChartArea.AxisY.LineWidth = 4;

        }


        //计算休市期间的Lable
        public static void CustomerPauseLabel(StockQuote StockQuote, Series SeriesStockQuote)
        {
            //加入空点
            List<string> fullQuoteDateList=AddEmptyPoints(StockQuote, SeriesStockQuote);

            if (StockQuote != null && StockQuote.QuoteDateList != null)
            {
                for (int i = 0; i < fullQuoteDateList.Count; i++)
                {
                    if (fullQuoteDateList[i].Contains(StockQuote.PauseStartTime.ToShortTimeString()))
                    {
                        SeriesStockQuote.Points[i].AxisLabel = string.Format("{0}/{1}", StockQuote.PauseStartTime.ToShortTimeString(), StockQuote.PauseEndTime.ToShortTimeString());
                        SeriesStockQuote.Points[i].BorderColor = Color.White;
                    }
                    else
                    {

                        if (fullQuoteDateList[i].Contains(StockQuote.PauseEndTime.ToShortTimeString()))
                        {
                            SeriesStockQuote.Points[i].AxisLabel = string.Format("{0}/{1}", StockQuote.PauseStartTime.ToShortTimeString(), StockQuote.PauseEndTime.ToShortTimeString());
                            SeriesStockQuote.Points[i].BorderColor = Color.White;
                        }
                    }

                }
            }

            SeriesStockQuote.EmptyPointStyle.BorderWidth = 0;
            SeriesStockQuote.EmptyPointStyle.CustomAttributes = "EmptyPointValue = Zero";
        }



        public static List<string> AddEmptyPoints(StockQuote StockQuote, Series mySeries)
        {
            List<string> fullQuoteDateList = new List<string>();
            if (StockQuote.QuoteDateList != null)
            {

                fullQuoteDateList = StockQuote.QuoteDateList.FindAll(p => true);

                //添加空点               

                DateTime t = DateTime.Now;
                t = DateTime.Parse(string.Format("{0} {1}", StockQuote.QuoteStartTime.ToShortDateString(), StockQuote.CurrentDate));

                //创建行情模板
                List<string> QuoteDateListTemp = CreateQuoteDateListTemplate(StockQuote);


                for (int i = 0; i < QuoteDateListTemp.Count; i++)
                {
                    bool isHave = StockQuote.QuoteDateList.Contains(QuoteDateListTemp[i]);
                    if (!isHave)//不含有
                    {
                        DateTime dt = DateTime.Parse(string.Format("{0} {1}", StockQuote.QuoteStartTime.ToShortDateString(), QuoteDateListTemp[i]));
                        if (dt >= t)
                        {
                            mySeries.Points.AddXY(QuoteDateListTemp[i], 0.0d);
                            int pointsCount = mySeries.Points.Count;
                            if (pointsCount > 0)
                            {
                                mySeries.Points[pointsCount - 1].Empty = true;
                            }
                            fullQuoteDateList.Add(QuoteDateListTemp[i]);
                        }
                        else
                        {
                            //计算临近值
                            double nearValue = 0.0d;
                            if (StockQuote.QuoteDateList.Count>0)
                            {
                                //DateTime nearTime = DateTime.Parse(string.Format("{0} {1}", StockQuote.QuoteStartTime.ToShortDateString(), StockQuote.QuoteDateList[i]));
                                var minSpan = (from date in StockQuote.QuoteDateList
                                               select Math.Abs((dt - DateTime.Parse(string.Format("{0} {1}", StockQuote.QuoteStartTime.ToShortDateString(), date))).TotalSeconds)).Min();

                                var priceList = (from date in StockQuote.QuoteDateList
                                                 where Math.Abs((dt - DateTime.Parse(string.Format("{0} {1}", StockQuote.QuoteStartTime.ToShortDateString(), date))).TotalSeconds) == minSpan
                                                 select StockQuote.QuotePriceList[StockQuote.QuoteDateList.IndexOf(date)]).ToList();
                                if (priceList != null && priceList.Count > 0)
                                {
                                    nearValue = priceList[0];
                                }
                            }

                            mySeries.Points.InsertXY(i, QuoteDateListTemp[i], nearValue);
                            fullQuoteDateList.Insert(i, QuoteDateListTemp[i]);                                 
                        }

                    }
                }
            }
            return fullQuoteDateList;
            
        }

        private static void FindNearPoint(StockQuote StockQuote,string quoteTime)
        {
            //StockQuote.QuoteDateList
        }

        //创建行情模板
        public static List<string> CreateQuoteDateListTemplate(StockQuote StockQuote)
        {
            List<string> QuoteDateListTemp = new List<string>();
            int left = int.Parse((StockQuote.PauseStartTime - StockQuote.QuoteStartTime).TotalMinutes.ToString("0")) + 1;
            int right = int.Parse((StockQuote.QuoteEndTime - StockQuote.PauseEndTime).TotalMinutes.ToString("0"));

            for (int i = 0; i < left; i++)
            {
                string str = StockQuote.QuoteStartTime.AddMinutes(i).ToShortTimeString();
                QuoteDateListTemp.Add(str);
            }
            for (int i = 0; i < right; i++)
            {
                string str = StockQuote.PauseEndTime.AddMinutes(i + 1).ToShortTimeString();
                QuoteDateListTemp.Add(str);
            }//模板构建完毕 本该含有的点
            return QuoteDateListTemp;
        }

    }
}
